#!/usr/bin/env bash
# vim: set ft=sh:

font="${BAR_FONT:-"-*-lemon-*"}"
icon="${BAR_ICON:-"-*-siji-*"}"
height="${BAR_HEIGHT:-36}"


get_workspaces() {
    # Create a dynamic workspace switcher.
    workspaces="$(wmctrl -d | awk -v fg="${color3:-#F0F0F0}" \
                                  '$6!="0,0"{next}
                                   $2=="-"{printf "  %{F"fg"}" $NF "  %{F}"}
                                   $2=="*"{printf "  " $NF "  "}')"
    printf "%s\\n" "$workspaces"
}


get_window() {
    # Get title of focused window.
    printf "%.75s\\n" "$(xdotool getwindowfocus getwindowname)"
}


get_date() {
    # Get the date and time.
    printf "%s\\n" "$(date +"%a %d %b - %H:%M")"
}


get_song() {
    # Get the current song from mopidy/mpd.
    printf "%.100s\\n" "$(mpc current -f '%albumartist% - %title%')"
}

get_dbx() {
    dbx="$(echo -e $(dropbox-cli status | sed -n 1p))"

    dbx_icon=""

    printf "%s\\n" "$(icon "$dbx_icon") ${dbx}"
}

get_mail() {
    # Get new mail from offlineimap folder.
    mail="$(echo -e $(ls /home/ajibola/.mail/Gmail/INBOX/new | wc -l))"

    mail_icon=""

    printf "%s\\n" "$(icon "$mail_icon") ${mail}"
}

get_volume() {
    # Get the system volume.
    volume="$(amixer sget Master | grep -o -m 1 -E "[[:digit:]]+%")"

    case "$volume" in
        0%|[0-9]%)   vol_icon=" " ;;
        1?%|2?%|3?%) vol_icon="" ;;
        4?%|5?%|6?%) vol_icon="" ;;
        *) vol_icon="" ;;
    esac

    printf "%s\\n" "$(icon "$vol_icon") ${volume}"

}

get_bat() {
    status=$(cat /sys/class/power_supply/BAT0/status)
    capacity=$(cat /sys/class/power_supply/BAT0/capacity)

    if [ "$status" == "Charging" ]; then
        printf "%s\\n" "$(icon "") ${capacity}"
    elif [ "$status" == "Full" ]; then
        printf "%s\\n" "$(icon "") ${capacity}"
    elif (("$capacity" <= "20")); then
        printf "%s\\n" "$(icon2 "") ${capacity}"
    elif (("$capacity" <= "60")); then
        printf "%s\\n" "$(icon "") ${capacity}"
    elif (("$capacity" <= "100")); then
        printf "%s\\n" "$(icon "") ${capacity}"
    fi
}

get_wifi() {
    wifi="$(echo -e $(iw wlp1s0 link | grep 'SSID' | cut -c 8-))"
    wifi_icon=""

    printf "%s\\n" "$(icon "$wifi_icon") ${wifi}"
}


icon() {
    # Format an icon.
    printf "%s\\n" "%{F${color8:-#F0F0F0}}${1}%{F}"

}

icon2() {
    # Format an icon.
    printf "%s\\n" "%{F${color11:-#F0F0F0}}${1}%{F}"
}


main() {
    # Main script function.

    # Import colors from 'nord'.
    . "${HOME}/.Xresources.d/colors/nord/nord.sh"

    # Loop and print the info.
    while :; do
        workspaces="$(get_workspaces)"
        window="$(icon "") $(get_window)"
        date="$(get_date)"
        volume="$(get_volume)"
        bat="$(get_bat)"
        wifi="$(get_wifi)"

        printf "%s%s%s\\n" \
               "%{l}   ${workspaces}  |  ${window}" \
               "%{c}${date}" \
               "%{r}TESTE  |  ${volume} ${bat} ${wifi}    "
        sleep .1s
    done |\

    # Launch lemonbar.
    lemonbar -g "x${height}" \
             -B "${color0:-#000000}" -F "${color4:-#F0F0F0}"\
             -n "bar" -f "$font" -f "$icon"
}

main "$@"
